//
//  RCUserScoreCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserScoreCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "UIImageView+WebCache.h"

@implementation RCUserScoreCell{
    UIImageView *_picImageView;
    UILabel     *_titleLabel;
    UILabel     *_scoreLabel;
    int64_t     _reqID;
    
}

@synthesize staffID = _staffID;
@synthesize delegate= _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolder];
    }
    return self;
}




- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    KF_RELEASE_SAFELY(_staffID);
    [super dealloc];
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) createPlaceHolder
{
    _picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 60, 50)];
    [self.contentView addSubview:_picImageView];
    [_picImageView release];
    
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 20, 180, 20)];
    [self.contentView addSubview:_titleLabel];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    _titleLabel.textColor = kNormalTextColor;
    [_titleLabel release];
    
    
    UILabel *score = [[UILabel alloc] initWithFrame:CGRectMake(85, 45, 45, 15)];
    [self.contentView addSubview:score];
    score.backgroundColor = [UIColor clearColor];
    score.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    score.textColor = kNormalTextColor;
    score.text = @"积分：";
    [score release];
    
    
    _scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 45, 30, 15)];
    [self.contentView addSubview:_scoreLabel];
    _scoreLabel.backgroundColor = [UIColor clearColor];
    _scoreLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    _scoreLabel.textColor = kNormalOrangeTextColor;
    _scoreLabel.textAlignment = NSTextAlignmentRight;
    [_scoreLabel release];
    
    
    UILabel *scoreEnd = [[UILabel alloc] initWithFrame:CGRectMake(160, 45, 45, 15)];
    [self.contentView addSubview:scoreEnd];
    scoreEnd.backgroundColor = [UIColor clearColor];
    scoreEnd.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    scoreEnd.textColor = kNormalTextColor;
    scoreEnd.text = @"积分";
    [scoreEnd release];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(250, 50, 60, 23)];
    [self.contentView addSubview:btn];
    [btn addTarget:self action:@selector(takeScore) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"兑换" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
//    [btn setBackgroundColor:kNormalOrangeTextColor];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn-bg-yellow"] forState:UIControlStateNormal];
    [btn release];
    
    UIView *horizonLine = [[UIView alloc]initWithFrame:CGRectMake(15, 79, 320-30, 1)];
    horizonLine.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
    [self.contentView addSubview:horizonLine];
    [horizonLine release];
    
    
    
}


- (void)takeScore
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestStaff:self.staffID WithDelegte:self];
}

- (void) bindData:(RCScoreListItemDataModel *)item
{
    UIImage *img = [UIImage imageNamed:@"image_member_cardType01"];
    self.staffID = item.scoreID;
    [_picImageView setImageWithURL:[NSURL URLWithString:item.picUrl] placeholderImage:img];
    _titleLabel.text = item.title;
    _scoreLabel.text = item.points;
    
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if ([_delegate respondsToSelector:@selector(didGetGoodsSuccess:withMessage:)]) {
        [_delegate didGetGoodsSuccess:NO withMessage:@"兑换失败"];
    }
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        if ([_delegate respondsToSelector:@selector(didGetGoodsSuccess:withMessage:)]) {
            [_delegate didGetGoodsSuccess:YES withMessage:nil];
        }
    }else{
        if ([_delegate respondsToSelector:@selector(didGetGoodsSuccess:withMessage:)]) {
            [_delegate didGetGoodsSuccess:NO withMessage:dataModel.errorMsg];
        }
    }
    
}



@end
